
<template>
    <div>
        <Tree :data="baseData" :load-data="loadData" :render="renderContent"></Tree>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                baseData:[
                    
                ]
            };
        },
        mounted() {
            let qs = require('qs');
            this.axios({
                method: "post",
                url: `${this.adminUrl}/getFirst`,
                data: qs.stringify({})
                }).then(response => {
                    if ((response.data.result == 200)) {
                        let data = [];
                        let json = response.data.data;                        
                        for (let i = 0; i < json.length; i++){
                            let level = parseInt(json[i]["level"]);
                            switch(level){
                                case 1:
                                    level = "联合创始人";
                                break;
                                case 2:
                                    level = "执行董事";
                                break;
                                case 3:
                                    level = "合伙人";
                                break;
                                case 4:
                                    level = "钻石代理";
                                break;
                            }
                            data.push({title:json[i]["name"] + "("+ level +":" + json[i]["uid"] + ")", loading: false, children:[]});
                        }
                        this.baseData = data;
                    }else{
                        alert("请求失败");
                    }
                }).catch(err =>{
                    console.log(err);
            });
        },
        methods: {
            next(num, root){
                for (let i = 0; i < root.length; i++) { 
                    //console.log(root[i].parent);
                    if (root[i].parent >= 0) {                        
                        num[root[i].nodeKey] = num[root[i].parent] + 1;
                    }else{
                        num[root[i].nodeKey] = 0;
                    }                                                     
                }               
            },
            renderContent(h, {root, node, data}){
                let num = {};    
                this.next(num, root); 
                if (num[node.node.nodeKey] == 0) {
                    return h('span', {
                        
                    },[
                        h('span', data.title.substr(0,data.title.indexOf('('))),
                        h('span', data.title.substr(data.title.indexOf('(')))
                    ]);
                }
                switch(num[node.node.nodeKey] % 3){
                    case 1:
                    return h('span',[
                        h('span',{
                            style:{
                                color:'red'
                            }
                        }, data.title.substr(0,data.title.indexOf('('))),
                        h('span', data.title.substr(data.title.indexOf('(')))
                    ]);
                    case 2:
                    return h('span',[
                        h('span',{
                            style:{
                                color:'#f90'
                            }
                        }, data.title.substr(0,data.title.indexOf('('))),
                        h('span', data.title.substr(data.title.indexOf('(')))
                    ]);
                    case 0:
                    return h('span',[
                        h('span',{
                            style:{
                                color:'blue'
                            }
                        }, data.title.substr(0,data.title.indexOf('('))),
                        h('span', data.title.substr(data.title.indexOf('(')))
                    ]);
                }               
            },
            loadData(item, callback){
                let s = item["title"];
                s = s.substr(s.indexOf(':') + 1, 11);
                let qs = require('qs');
                this.axios({
                    method: "post",
                    url: `${this.adminUrl}/getDowner`,
                    data: qs.stringify({uid:`${s}`})
                    }).then(response => {
                        if ((response.data.result == 200)) {
                            let data = [];
                            let json = response.data.data;
                            for (let i = 0; i < json.length; i++){
                                let level = parseInt(json[i]["level"]);
                                switch(level){
                                    case 1:
                                        level = "联合创始人";
                                    break;
                                    case 2:
                                        level = "执行董事";
                                    break;
                                    case 3:
                                        level = "合伙人";
                                    break;
                                    case 4:
                                        level = "钻石代理";
                                    break;
                                }
                                data.push({title:json[i]["name"] + "("+ level +":" + json[i]["uid"] + ")",loading: false,children:[]});
                            } 
                            callback(data);                          
                        }else{
                            alert("请求失败");
                        }
                    }).catch(err =>{
                        console.log(err);
                });
            }
        }
    };
</script>

   



<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
